import asyncio
import time
import tkinter
from selenium import webdriver

from view.run.scriptRun import run
from view.inventory.allot.AllotDetail import testAllotDetail
from view.inventory.allot.AllotEditor import testAllotEditor
from view.inventory.allot.AllotOrder import testAllotOrder
from view.inventory.finished.FinishedDetail import testFinishedDetail
from view.inventory.finished.FinishedEditor import testFinishedEditor
from view.inventory.finished.FinishedOrder import testFinishedOrder
from view.inventory.materials.MaterialsDetail import testMaterialsDetail
from view.inventory.materials.MaterialsEditor import testMaterialsEditor
from view.inventory.materials.MaterialsOrder import testMaterialsOrder
from view.inventory.purchase.PurchasePutStorageDetail import testPurchasePutStorageDetail
from view.inventory.purchase.PurchasePutStorageEditor import testPurchasePutStorageEditor
from view.inventory.purchase.PurchasePutStorageOrder import testPurchasePutStorageOrder
from view.inventory.workflow.AuditFlow import testAuditFlow
from view.purchase.PurchaseDetail import testPurchaseDetail
from view.purchase.PurchaseEditor import testPurchaseEditor
from view.purchase.PurchaseOrder import testPurchaseOrder
from view.sale.OrderBills import testOrderBills
from view.sale.OrderDetails import testOrderDetails
from view.sale.OrderEditor import testOrderEditor
from view.sale.sale.SaleBills import testSaleBills
from view.sale.sale.SaleDetails import testSaleDetails
from view.sale.sale.SaleEditor import testSaleEditor

# 默认url
defaultUrl = 'http://t6.asrj.net/'
# defaultUrl = 'http://localhost:8080/'
# 默认账号
defaultAccount = '17999999999'
# 默认密码
defaultPassword = '123456'


# 打开界面
def openInterface():
    root = tkinter.Tk(className='傲视易用v6自动化测试')  # 生成root主窗口
    root.geometry('440x250')  # 定义窗体的大小，是400X200像素
    # root.iconbitmap('favicon.ico')
    nowUrlText = tkinter.StringVar()  # 可以在Label变化的对象
    nowUrl = tkinter.Label(root, textvariable=nowUrlText).place(x=180, y=10)  # 访问地址
    tkinter.Label(root, text='地址:').place(x=150, y=10)  # 访问地址
    nowUrlText.set(defaultUrl)

    def changeNowUrlText1():
        nowUrlText.set('http://localhost:8080/')

    def changeNowUrlText2():
        nowUrlText.set('http://v6.asrj.net/')

    def changeNowUrlText3():
        nowUrlText.set('http://hm.asrj.net/')

    def changeNowUrlText4():
        nowUrlText.set('http://ryx.asrj.net/')

    def changeNowUrlText5():
        nowUrlText.set('http://tne.asrj.net/')

    def startClick():
        global defaultUrl
        global defaultAccount
        global defaultPassword
        defaultUrl = nowUrlText.get()
        defaultAccount = accountEntry.get()
        defaultPassword = passwordEntry.get()
        asyncio.run(start())

    def closeClick():
        exit()

    localB = tkinter.Button(root, text="本地", command=changeNowUrlText1, width=10).place(x=20, y=44)
    v6B = tkinter.Button(root, text="测试", command=changeNowUrlText2, width=10).place(x=100, y=44)
    hmB = tkinter.Button(root, text="hm", command=changeNowUrlText3, width=10).place(x=180, y=44)
    ryxB = tkinter.Button(root, text="ryx", command=changeNowUrlText4, width=10).place(x=260, y=44)
    tneB = tkinter.Button(root, text="tne", command=changeNowUrlText5, width=10).place(x=340, y=44)
    # nowUrl.pack()
    # localB.pack()
    # v6B.pack()
    # hmB.pack()
    # ryxB.pack()
    # tneB.pack()
    tkinter.Label(root, text='账号:').place(x=35, y=102)
    accountEntry = tkinter.Entry(root, bd=5)
    accountEntry.pack()
    accountEntry.place(x=80, y=100)
    accountEntry.insert(0, defaultAccount)

    tkinter.Label(root, text='密码:').place(x=35, y=152)
    passwordEntry = tkinter.Entry(root, bd=5)
    passwordEntry.pack()
    passwordEntry.place(x=80, y=152)
    passwordEntry.insert(0, defaultPassword)

    tkinter.Button(root, text="启动", command=startClick, width=15).place(x=285, y=102)
    tkinter.Button(root, text="关闭", command=closeClick, width=15).place(x=285, y=152)

    # 进入消息循环体
    root.mainloop()

# 启动测试
async def start():
    print("开始测试！=>url:", defaultUrl)
    browser = webdriver.Chrome()
    # browser.set_window_size(1440, 900)#浏览器1440*900
    browser.maximize_window()  # 全屏
    browser.get(defaultUrl)
    # 卡一个加载时间
    # time.sleep(1)
    browser.find_element_by_xpath("//*[@placeholder = '请输入登陆帐号']").send_keys(defaultAccount)
    browser.find_element_by_xpath("//*[@placeholder = '请输入登陆密码']").send_keys(defaultPassword)
    browser.find_element_by_xpath("//*[@class='el-button el-button--primary']").click()  # 点击登录
    time.sleep(5)

    await run(browser)
    # while 1:
        # await testPurchaseEditor(browser)  # 采购订货单据
        # await testPurchaseOrder(browser)  # 采购单据列表
        # await testPurchaseDetail(browser)  # 采购明细列表
        # await testPurchasePutStorageEditor(browser)  # 采购入库单据
        # await testPurchasePutStorageOrder(browser)  # 采购入库单据列表
        # await testPurchasePutStorageDetail(browser)  # 采购入库明细列表
        # await testAllotEditor(browser)  # 调拨单据
        # await testAllotOrder(browser)  # 调拨入库单据列表
        # await testAllotDetail(browser)  # 调拨入库明细列表
        # await testMaterialsEditor(browser)  # 存货损益单据
        # await testMaterialsOrder(browser)  # 存货损益单据列表
        # await testMaterialsDetail(browser)  # 存货损益明细列表
        # await testOrderEditor(browser)  # 订货单据
        # # await testOrderBills(browser)  # 订货单据列表
        # # await testOrderDetails(browser)  # 订货明细列表
        # await testAuditFlow(browser)  # 审核流程
        # await testFinishedEditor(browser)  # 成品入库单据
        # await testFinishedOrder(browser)  # 成品入库单据列表
        # await testFinishedDetail(browser)  # 成品入库单据明细
        # await testSaleEditor(browser)  # 销售单据
        # await testSaleBills(browser) #销售单据列表
        # await testSaleDetails(browser) #销售明细列表
        # input('测试完成——输入任意键退出。。。')

async def startT():
    browser = webdriver.Chrome()
    # browser.set_window_size(1440, 900)#浏览器1440*900
    browser.maximize_window()  # 全屏
    browser.get(defaultUrl)
    # 卡一个加载时间
    time.sleep(1)
    browser.find_element_by_xpath("//*[@placeholder = '请输入登陆帐号']").send_keys(defaultAccount)
    browser.find_element_by_xpath("//*[@placeholder = '请输入登陆密码']").send_keys(defaultPassword)
    browser.find_element_by_xpath("//*[@class='el-button el-button--primary']").click()  # 点击登录
    time.sleep(5)
    await testOrderEditor(browser)  # 订货单据
    for i in range(99):
        print('循环第'+str(i+1)+'次！')
        await testOrderEditor(browser,True)  # 订货单据


if __name__ == '__main__':
    # openInterface()
    asyncio.run(start())
    # asyncio.run(startT())
